<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class article_mod extends module
{
    function get_all($_conf)
    {
        $page = isset($_conf['page']) ? $_conf['page'] : '1';
        $page_size = isset($_conf['page_size']) ? $_conf['page_size'] : '12';
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '';
        $key = isset($_GET['key']) ? $_GET['key'] : '';
        $category = $this->category_list();
        $wsql = '';
        if (!empty($cid)) {
            $wsql .= ' cid in (' . $this->category_in($cid) . ') and ';
        }
        if (!empty($key)) {
            $wsql .= " title like '%" . $key . "%' and ";
        }
        if (trim($wsql) != '') {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $data['cname'] = $category[$cid]['name'];
        $query = $this->db->get_all("select * from " . table('article') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $k => $v) {
                if (isset($category[$v['cid']])) {
                    $v['cname'] = $category[$v['cid']]['name'];
                }
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(*) as total from " . table('article') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            return $data;
        } else {
            return $data;
        }
    }
    public function category_in($cid)
    {
        $aaa = $this->category_in_box($this->category(), $cid);
        $result_cid = isset($aaa) ? $cid . ',' . trim($aaa, ',') : $cid;
        return $result_cid;
    }
    public function category_in_box($data, $cid)
    {
        foreach ($data as $v) {
            if ($v['pcid'] == $cid) {
                $cats_in .= $v['cid'] . ',';
                $cats_in .= $this->category_in_box($data, $v['cid']);
            }
        }
        if (isset($cats_in)) {
            return $cats_in;
        }
    }
    function browser($id)
    {
        $id = isset($id) ? $id : false;
        if ($id) {
            $this->db->query("update " . table('article') . " set click = click+1 where id = " . $id);
        }
    }
    function get_one($arr)
    {
        $id = !empty($arr['id']) ? $arr['id'] : exit('ID 不能为空！');
        $row = $this->db->get_one("select * from " . table('article') . " where id = " . $id);
        if ($row) {
            $wsql = '';
            if ($row['cid']) {
                $wsql = ' and cid = ' . $row['cid'];
            }
            $prev_row = $this->db->get_one("select id,cid,title from " . table("article") . " where id < " . $id . $wsql . " order by id desc limit 1");
            $next_row = $this->db->get_one("select id,cid,title from " . table("article") . " where id > " . $id . $wsql . " order by id asc limit 1");
            $row['prev_id'] = $prev_row['id'];
            $row['prev_title'] = $prev_row['title'];
            $row['next_id'] = $next_row['id'];
            $row['next_title'] = $next_row['title'];
            $row['addtime'] = _date($row['addtime']);
            return $row;
        } else {
            return false;
        }
    }
    public function about_list($arr)
    {
        $id = isset($arr['id']) ? $arr['id'] : false;
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : 10;
        if ($id) {
            $wsql = " where id != " . $id;
        } else {
            $wsql = '';
        }
        $rall = $this->db->get_all("select * from " . table('article') . $wsql . " order by click desc limit " . $page_size);
        if ($rall) {
            foreach ($rall as $k => $v) {
                $data[] = $v;
            }
            return $data;
        } else {
            return false;
        }
    }
    private function category()
    {
        $cats = $this->db->get_all("select * from " . table('article_category') . " where status = 1 order by px desc");
        return $cats;
    }
    public function category_list()
    {
        $cats = $this->category();
        if ($cats) {
            foreach ($cats as $k => $v) {
                $result[$v['cid']] = $v;
            }
            return $result;
        } else {
            return false;
        }
    }
    public function category_plist($arr)
    {
        $cid = !empty($arr['cid']) ? $arr['cid'] : 0;
        $cats = $this->category();
        if ($cats) {
            foreach ($cats as $k => $v) {
                $carr[$v['cid']] = $v;
                $parr[$v['pcid']][] = $v;
            }
            if (isset($parr[$cid])) {
                return $parr[$cid];
            } else {
                return $parr[$carr[$cid]['pcid']];
            }
        } else {
            return false;
        }
    }
    function category_up_name($cid)
    {
        $cats = $this->category();
        if ($cats) {
            $result = $this->category_up_name_loop($cats, $cid);
            sort($result);
            return $result;
        }
    }
    private function category_up_name_loop($cats, $cid)
    {
        static $ttl_nav = array();
        foreach ($cats as $k => $v) {
            if ($cid == $v['cid']) {
                $ttl_nav[] = array('cid' => $v['cid'], 'name' => $v['name']);
                $this->category_up_name_loop($cats, $v['pcid']);
            }
        }
        return $ttl_nav;
    }
    function get_list($arr)
    {
        $aid = isset($arr['aid']) ? $arr['aid'] : exit('article id is not null');
        $page = isset($arr['page']) ? $arr['page'] : '1';
        $page_size = isset($arr['page_size']) ? $arr['page_size'] : '20';
        $list = $this->db->get_all("select c.*,u.uid,u.avatar,u.nick from " . table('article_comment') . " as c left join " . table('member') . " as u on u.uid = c.uid where c.aid = " . $aid . " order by c.id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($list) {
            $total = $this->db->get_one("select count(*) as total from " . table('article_comment') . " where aid = " . $aid);
            $data['list'] = @$list;
            $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            return $data;
        } else {
            return false;
        }
    }
}